package com.wlr.study.leetcode;

import java.util.HashMap;
import java.util.Map;

/**
 * 最长子序列
 *
 * @author HustleLr
 * @version 1.0.0
 * @date 2024/1/17 9:27
 * @describe LeetCode128
 */
public class LeetCode128 {
    public int longestConsecutive(int[] nums) {
        Map<Integer, Integer> map = new HashMap<>();
        for (int num : nums) {
            if (map.containsKey(num)) {
                continue;
            }
            Integer big = map.getOrDefault(num + 1, 0);
            Integer small = map.getOrDefault(num - 1, 0);
            int current = big + small + 1;
            if (big != 0) {
                map.put(num + big, current);
            }
            if (small != 0) {
                map.put(num - small, current);
            }
            map.put(num, current);
        }
        Integer max = 0;
        for (Integer value : map.values()) {
            if (value > max) {
                max = value;
            }
        }
        return max;
    }
}
